home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: fopen not working
- Date: Mon, 12 Feb 96 20:55:53 GMT
- Organization: none
- Message-ID: <824158553snz@genesis.demon.co.uk>
- References: <4fjnj1$4q@fig.leba.net>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4fjnj1$4q@fig.leba.net> fweaver@leba.net writes:
-
- >Good evening to all C programers
- >
- > I have a problem I thought someone might be able to shed some light on
- >for me. Below is some of the code I wrote for an install program.
- >
- >
- >#include<stdio.h>
- >#include<conio.h>
-
- There doesn't appear to be anything here that need conio.h. Also it isn't
- a standard C header file so is best avoided when posting to comp.lang.c
-
- >char buf[125],stwd[110],trwd[110],*pt,*pt1;
- >int i,vr,vd;
- >FILE *in, *out;
- >main()
- >{ [snip]
- >
- > instlfile("trkr.cf_","trkr.cfg");
- > instlfile("maplvl3.da_","maplvl3.dat");
- > instlfile("atpctrkr.do_","atpctrkr.doc");
- > instlfile("readme.1s_","readme.1st");
- > instlfile("file_id.di_","file_id.diz");
- >
- > [snip]
- >
-
- > if((in=fopen(buf,"r"))!=NULL)
- > { do{ fgets(buf,120,in);
- > pt= (char *)strstr(buf,"XXXXX");
- > if(pt!=NULL){ fclose(out); pt=pt+5;if((out=fopen(pt,
- >"w"))==NULL)printf("ERROR");
- > else{printf("\nInstalling %s",pt);continue; }}
- > fputs(buf,out);
- > }while(feof(in)==0);
- > }
- >}
-
- This is almost impossible to read. I've reformatted into something clearer.
- It isn't the idea of C to cram a program into as few lines as possible.
-
- >> if ((in=fopen(buf,"r")) != NULL) {
- >> do {
- >> fgets(buf,120,in);
-
- What is 120? If you want to pass the size of buf then use sizeof buff, or
- perhaps better define a constant and use that in both places.
-
- fgets can fail and return NULL which you need to test then and there. This
- sort of loop normally takes the form:
-
- while (fgets(buf, sizeof buf, in) != NULL) {
- ...
- }
-
- >> pt = (char *)strstr(buf,"XXXXX");
-
- Why the cast? Did you include string.h? If not your compiler probably produced
- a warning. The cast may stop the warning being generated but the compiler
- will still generate code as if strstr() returned an int.
-
- >> if (pt != NULL) {
- >> fclose(out);
-
- You'd better make sure that out contains a valid file pointer value e.g.
- NULL is not legal here. Check especially the first pass through the loop.
-
- >> pt=pt+5;
-
- The normal C idiom is pt += 5;
-
- >> if ((out=fopen(pt, "w")) == NULL)
-
- That looks OK. Are you sure it is exactly what you have in your code e.g.
- not == instead of =?
-
- >> printf("ERROR");
- >> else {
- >> printf("\nInstalling %s",pt);
- >> continue;
- >> }
- >> }
- >> fputs(buf, out);
- >> } while (feof(in)==0);
-
- It is very rarely that you need to use feof in C. Anyway this is far too
- late since by the time you've detected that the line hasn't been read in
- you've already tried to process it. The while (fgets() != NULL) loop above
- is the correct approach.
-
- >> }
- >>}
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-